package io.framework.modules.hpinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.hpinfo.dao.VTHpglHpspbhqjfjqDao;
import io.framework.modules.hpinfo.dao.VTHpglProjectinfoDao;
import io.framework.modules.hpinfo.dto.VTHpglHpspbhqjfjqDTO;
import io.framework.modules.hpinfo.entity.VTHpglHpspbhqjfjqEntity;
import io.framework.modules.hpinfo.entity.VTHpglProjectinfoEntity;
import io.framework.modules.hpinfo.service.VTHpglHpspbhqjfjqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 环境影响评价管理-环评审批保护区及风景区情况
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-19
 */
@Service
public class VTHpglHpspbhqjfjqServiceImpl extends CrudServiceImpl<VTHpglHpspbhqjfjqDao, VTHpglHpspbhqjfjqEntity, VTHpglHpspbhqjfjqDTO> implements VTHpglHpspbhqjfjqService {

    @Autowired
    private VTHpglHpspbhqjfjqDao hpspbhqjfjqDao;

    @Autowired
    private VTHpglProjectinfoDao projectinfoDao;

    @Override
    public QueryWrapper<VTHpglHpspbhqjfjqEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<VTHpglHpspbhqjfjqEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public int bindProjectInfo(Map<String, Object> params) {

        return baseDao.bindProjectInfo(params);
    }

    @Override
    public PageData<VTHpglHpspbhqjfjqDTO> pages(Map<String, Object> params) {

        IPage<VTHpglHpspbhqjfjqEntity> pages = getPage(params, Constant.CREATE_TIME, false);

        List<VTHpglHpspbhqjfjqEntity> list = baseDao.getList(params);

        return getPageData(list, pages.getTotal(), VTHpglHpspbhqjfjqDTO.class);
    }

    @Override
    public VTHpglHpspbhqjfjqDTO getProjectInfoByProjectId(String id){
        //找建设单位
        VTHpglProjectinfoEntity projectEntity = projectinfoDao.selectById(id);

        List<VTHpglHpspbhqjfjqEntity> hpspbhqjfjqEntitys = hpspbhqjfjqDao.getProjectInfoByProjectId(id, projectEntity.getProjectXmmc());
        VTHpglHpspbhqjfjqEntity entity = new VTHpglHpspbhqjfjqEntity();
        if(!CollectionUtils.isEmpty(hpspbhqjfjqEntitys)){
            entity = hpspbhqjfjqEntitys.get(0);
        }
        return ConvertUtils.sourceToTarget(entity,VTHpglHpspbhqjfjqDTO.class);
    }

}